package com.da.lon.wang.xlg.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.da.lon.wang.xlg.MyApplication;
import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.bean.ResultSearchBean.ResultSearchListBean.SearchResultList;
import com.da.lon.wang.xlg.util.ViewHolder;

import java.util.List;

public class SearchGridAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private List<SearchResultList> searchResultLists;
    private Context context;

    public SearchGridAdapter(List<SearchResultList> list, Context context) {
        inflater = LayoutInflater.from(context);
        this.context = context;
        this.searchResultLists = list;

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return searchResultLists != null ? searchResultLists.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return searchResultLists.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        try {
            SearchResultList searchResultList = searchResultLists.get(position);

            convertView = inflater.inflate(R.layout.ouka_search_griditem, null);

            ImageView imgView = ViewHolder.get(convertView,
                    R.id.search_grid_img);
            LinearLayout xiadan_ll = ViewHolder.get(convertView, R.id.xiadan_ll);
            TextView nameView = ViewHolder.get(convertView,
                    R.id.search_grid_name);
            TextView pricespeView = ViewHolder.get(convertView,
                    R.id.search_grid_pricespe);
            TextView priceView = ViewHolder.get(convertView,
                    R.id.search_grid_price);
            TextView zkView = ViewHolder.get(convertView, R.id.search_grid_zk);

            ImageView lefttopView = ViewHolder.get(convertView,
                    R.id.search_grid_lefttop);

            imgView.setScaleType(ScaleType.CENTER_CROP);

            MyApplication.loadImageByPicasso(context, searchResultList.getBigimage(), null, imgView, 300, 300, 0);
            nameView.setText(searchResultList.getName());
            pricespeView.setText("￥" + searchResultList.getPricespe());
            priceView.setText("￥" + searchResultList.getPrice());
            priceView.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            if (searchResultList.getZkl() > 0) {
                zkView.setText(searchResultList.getZkl() + "折");
            } else {
                xiadan_ll.setVisibility(View.GONE);
            }

            if ("1".equals(searchResultList.getIsnew())) {
                lefttopView.setImageResource(R.drawable.ouka_list_isnew);
            } else if ("1".equals(searchResultList.getIshot())) {
                lefttopView.setImageResource(R.drawable.ouka_list_ishot);
            } else {
                lefttopView.setVisibility(View.GONE);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return convertView;
    }

}
